package com.douwong.student.activity;


import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.base.BaseActivity;
import com.douwong.student.contant.CommonConfig;
import com.douwong.student.fragment.ClassTestRecordFragment;
import com.douwong.student.view.CommSearch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课堂考试记录
 */

public class ClassRoomTestActivity extends BaseActivity {
    private static final String mTAG = "ClassRoomTestActivity";
    private LinearLayout ll_error_test, ll_error_table,prepare_all_ll,delete_time_ll;
    private ImageView iv_error_test, iv_erroe_table, go_back_img;
    private TextView tv_error_test, tv_error_table;
    private CommSearch commSearch;
    private ImageView time_search;
    private TextView current_subject,select_time;
    public Map<String, Object> mNavDataSelect;
    private List<Map<String, Object>> mNavData;
    public static void startActivity(Context context){
        Intent intent=new Intent(context,ClassRoomTestActivity.class);
        context.startActivity(intent);

    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_class_room_test);
        select_time=(TextView) findViewById(R.id.select_time);
        current_subject=(TextView) findViewById(R.id.classroom_chooseSubject);
        time_search=(ImageView) findViewById(R.id.time_search);
        go_back_img = (ImageView) findViewById(R.id.go_back_img);
        ll_error_test = (LinearLayout) findViewById(R.id.ll_error_test);
        ll_error_table = (LinearLayout) findViewById(R.id.ll_error_table);
        iv_error_test = (ImageView) findViewById(R.id.error_test_img);
        iv_erroe_table = (ImageView) findViewById(R.id.iv_erroe_table);
        tv_error_test = (TextView) findViewById(R.id.tv_error_test);
        tv_error_table = (TextView) findViewById(R.id.tv_erroe_table);
        prepare_all_ll=(LinearLayout) findViewById(R.id.prepare_all_ll);
        delete_time_ll=(LinearLayout) findViewById(R.id.delete_time_ll);



    }

    @Override
    protected void initData() {
        super.initData();
        commSearch=new CommSearch(this);
        initListView();

    }

    @Override
    protected void initEvent() {
        super.initEvent();

        ClassOnFocusListener classOnFocusListener = new ClassOnFocusListener();
        ll_error_test.setOnFocusChangeListener(classOnFocusListener);
        AnswerOnClick onClick=new AnswerOnClick();
        time_search.setOnClickListener(onClick);
        go_back_img.setOnClickListener(onClick);
        prepare_all_ll.setOnClickListener(onClick);
        delete_time_ll.setOnClickListener(onClick);



    }
    private class AnswerOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                // 返回
                case R.id.go_back_img:
                    finish();
                    break;
                case R.id.prepare_all_ll:
                    commSearch.showSubject(current_subject, delete_time_ll, CommonConfig.TEST_SEARCH);
                    break;
                case R.id.time_search:
                    commSearch.showTimeWindow(delete_time_ll,select_time, CommonConfig.TEST_SEARCH);
                    break;
                case R.id.delete_time_ll:
                    delete_time_ll.setVisibility(View.GONE);
                    commSearch.setStartTime(null,CommonConfig.TEST_SEARCH);
                    break;

            }
        }

    }

    // 选项切换判断

    private class ClassOnFocusListener implements View.OnFocusChangeListener {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            switch (v.getId()) {
                // 测验
                case R.id.ll_error_test:
                    if (hasFocus) {
                        iv_error_test.setImageResource(R.mipmap.error_table_test);
                        ll_error_test.setBackgroundColor(getResources().getColor(
                                R.color.class_file_word));
                        tv_error_test.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(0));
                    } else {
                        //					iv_error_test.setImageResource(R.drawable.error_test_hid);
                        //					ll_error_test.setBackgroundColor(getResources().getColor(
                        //							R.color.class_file_leftbackground));
                        //					tv_error_test.setTextColor(0Xffffffff);
                    }
                    break;
                // 错题库
                case R.id.ll_error_table:
                    if (hasFocus) {
                        iv_erroe_table.setImageResource(R.mipmap.error_table);
                        ll_error_table.setBackgroundColor(getResources().getColor(
                                R.color.class_file_word));
                        tv_error_table.setTextColor(0Xff7d7d7d);
                        showFragment(mNavData.get(1));
                    } else {
                        iv_erroe_table.setImageResource(R.mipmap.error_table_hid);
                        ll_error_table.setBackgroundColor(getResources().getColor(
                                R.color.class_file_leftbackground));
                        tv_error_table.setTextColor(0Xffffffff);
                    }
                    break;

            }
        }
    }
    /**
     * 显示内容
     *
     * @param map
     */
    public void showFragment(Map<String, Object> map) {
        mNavDataSelect = map;
        FragmentManager fragmentManager = getSupportFragmentManager();
        // mainNavAdapter.notifyDataSetChanged();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        Fragment fragment = (Fragment) map.get("fragment");
        boolean isInit = Boolean.parseBoolean(map.get("isInit").toString());

        // 检查是否存在
        if (!isInit) {
            transaction.add(R.id.frament, fragment);
            map.put("isInit", true);
            Log.d(mTAG, String.format("create new fragment[%s]:%s",
                    map.get("name"), map.get("title")));
        }

        // 隐藏&显示控件
        for (Map<String, Object> item : mNavData) {
            Fragment itemFragment = (Fragment) item.get("fragment");
            if (itemFragment == null) {
                continue;
            }
            if (itemFragment.equals(fragment)) {
                transaction.show(itemFragment);
            } else {
                transaction.hide(itemFragment);
            }
        }
        transaction.commit();
        Log.d(mTAG,
                String.format("show fragment[%s]:%s", map.get("name"),
                        map.get("title")));
    }


    private void initListView() {
        mNavData = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = new HashMap<String, Object>();
      //  map.put("fragment", new ClassTestFragment());
        map.put("fragment", new ClassTestRecordFragment());
        map.put("isInit", true);
        mNavData.add(map);
        this.initFragment();
    }

    private void initFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        for (Map<String, Object> item : mNavData) {
            Fragment itemFragment = (Fragment) item.get("fragment");
            boolean isInit = Boolean
                    .parseBoolean(item.get("isInit").toString());
            if (isInit && itemFragment != null) {
                transaction.add(R.id.frament, itemFragment);
                transaction.hide(itemFragment);
            }
        }

        transaction.commit();
    }

}
